package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.BatteryChargeModel;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.BatteryChargeModelService;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.utils.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 电池型号表(BatteryChargeModel)表控制层
 *
 * @author jing
 * @since 2020-12-04 09:38:13
 */
@RestController
@RequestMapping("/batterychargemodel")
public class BatteryChargeModelController {
    /**
     * 服务对象
     */
    @Resource
    private BatteryChargeModelService batteryChargeModelService;

    /**
     * 通过主键查询单条数据
     *
     * @param batteryModelId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer batteryModelId) {
        return JsonResult.ok(this.batteryChargeModelService.queryById(batteryModelId));
    }

    @PostMapping("/add")
    public JsonResult add(BatteryChargeModel batteryChargeModel) {
        /* 校验 */
        Assert.notNull(batteryChargeModel.getCapacity(), "ver.capacity.notNull");
        Assert.notNull(batteryChargeModel.getVol(), "ver.vol.notNull");
        Assert.hasLength(batteryChargeModel.getFactory(), "ver.factory.notNull");
        Assert.hasLength(batteryChargeModel.getBatterymodelNO(), "ver.batterymodelNO.notNull");
        batteryChargeModel.setAddTime(new Date());
        batteryChargeModelService.insert(batteryChargeModel);
        return JsonResult.ok();
    }

    @PostMapping("/edit")
    public JsonResult edit(BatteryChargeModel batteryChargeModel) {
        Assert.notNull(batteryChargeModel.getBatteryModelId(), "ver.batteryModelId.notNull");
        Assert.notNull(batteryChargeModel.getCapacity(), "ver.capacity.notNull");
        Assert.notNull(batteryChargeModel.getVol(), "ver.vol.notNull");
        Assert.hasLength(batteryChargeModel.getFactory(), "ver.factory.notNull");
        Assert.hasLength(batteryChargeModel.getBatterymodelNO(), "ver.batterymodelNO.notNull");
        batteryChargeModelService.update(batteryChargeModel);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    public JsonResult del(BatteryChargeModel batteryChargeModel) {
        Assert.notNull(batteryChargeModel.getBatteryModelId(), "ver.batteryModelId.notNull");
        batteryChargeModelService.deleteById(batteryChargeModel.getBatteryModelId());
        return JsonResult.ok();
    }

    @PostMapping("/queryPage")
    public JsonResult queryPage(BatteryChargeModel batteryChargeModel) {
        batteryChargeModel.setPage(new Page(batteryChargeModel.getPageNO(), batteryChargeModel.getRowCount()));
        PageHelperUtil.initPage(batteryChargeModel.getPage());
        PageInfo pageInfo = new PageInfo(batteryChargeModelService.queryPage(batteryChargeModel));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

}